<?php

namespace app\data\controller\total;

use app\data\model\BaseUserUpgrade;
use app\data\model\DataUser;
use app\data\model\DataUserBalance;
use app\data\model\DataUserCommission;
use app\data\model\DataUserRebate;
use app\data\model\ShopGoods;
use app\data\model\ShopOrder;
use think\admin\Controller;

/**
 * 商城数据报表
 * Class Portal
 * @package app\data\controller\total
 */
class Portal extends Controller
{
    /**
     * 商城数据报表
     * @auth true
     * @menu true
     */
    public function index()
    {
        $this->usersTotal = DataUser::mk()->cache(true, 60)->count();
        $this->goodsTotal = ShopGoods::mk()->cache(true, 60)->count();
        $this->orderTotal = ShopOrder::mk()->cache(true, 60)->whereRaw('status >= 4')->whereRaw('status<=6')->count();
        $amountTotal = ShopOrder::mk()->cache(true, 60)->whereRaw('status >= 4')->whereRaw('status<=6')->sum('amount_total');
        $this->amountTotal = sprintf("%.2f",$amountTotal/100);
        // 近十天的用户及交易趋势
        $this->days = $this->app->cache->get('portals', []);
        if (empty($this->days)) {
            for ($i = 15; $i >= 0; $i--) {
                $date = date('Y-m-d', strtotime("-{$i}days"));
                $totalamount = ShopOrder::mk()->whereLike('create_at', "{$date}%")->whereRaw('status>=4')->whereRaw('status<=6')->sum('amount_total');
                $dis_amount=DataUserCommission::mk()->whereLike('created_at', "{$date}%")->where(['type'=>0])->whereIn('status','0,1')->sum('commission_amount');
                $this->days[] = [
                    '当天日期' => date('m-d', strtotime("-{$i}days")),
                    '增加用户' => DataUser::mk()->whereLike('create_at', "{$date}%")->count(),
                    '订单数量' => ShopOrder::mk()->whereLike('create_at', "{$date}%")->whereRaw('status>=4')->whereRaw('status<=6')->count(),
                    '订单金额' => sprintf("%.2f",$totalamount/100),
                    '返利金额' => sprintf("%.2f",$dis_amount/100),
                ];
            }
            $this->app->cache->set('portals', $this->days, 60);
        }
        $this->fetch();
    }
}
